using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using TPL.Xna.Common;

namespace GoldDigger.Sprites
{
    internal class ClockSprite : TextSprite
    {
        private readonly ISceneManager _sceneManager;
        private int _totalSeconds;
        private DateTime _lastTime;
        private bool _run;

        public ClockSprite(ISceneManager sceneManager)
            : base("0")
        {
            _sceneManager = sceneManager;
        }

        public void Start(int totalSeconds)
        {
            _run = true;
            _totalSeconds = totalSeconds;
            _lastTime = DateTime.Now;
        }

        protected override void InternalUpdate(GameTime gameTime, Rectangle clientBounds)
        {
            if (_run)
            {
                var n = DateTime.Now;
                var d = n.Subtract(_lastTime);
                if (d.TotalSeconds >= 1)
                {
                    _lastTime = n;
                    _totalSeconds--;

                    if (_totalSeconds <= 0)
                    {
                        _sceneManager.HandlSceneTimeUp();
                        _totalSeconds = 0;
                    }
                    
                    SetText(_totalSeconds.ToString());
                }
            }
        }
    }
}
